//
//  XYJXCTableViewCell.m
//  徙游记-01
//
//  Created by hcios on 2017/7/12.
//  Copyright © 2017年 鹿林. All rights reserved.
//

#import "XYJXCTableViewCell.h"
#import "UIImageView+WebCache.h"

@implementation XYJXCTableViewCell

#pragma mark - 初始化方法
+(instancetype)cellWithTableView:(UITableView *)tableView{

    static NSString *identifier = @"CellID";

    XYJXCTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

    if (cell == nil) {
        cell = [[XYJXCTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.separatorInset = UIEdgeInsetsMake(0, 10, 0, 10);
    cell.layoutMargins = UIEdgeInsetsMake(0, 10, 0, 10);
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        //取消点击高亮状态
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setupSubViews];
        UITapGestureRecognizer *ges = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(ges:)];
        self.cImageView.userInteractionEnabled = YES;
        [self.cImageView addGestureRecognizer:ges];
    }
    return self;
}

-(void)ges:(UITapGestureRecognizer *)tap{
    if (self.delegate && [self.delegate respondsToSelector:@selector(click:)]) {
        [self.delegate click:tap];
    }
}


#pragma mark - setupSubViews
- (void)setupSubViews{
    
    self.userInteractionEnabled = YES;
    self.cImageView = [[UIImageView alloc] init];
    self.cImageView.layer.cornerRadius = 4;
    self.cImageView.layer.masksToBounds = YES;
    [self addSubview:self.cImageView];
    
    self.topLab = [[UILabel alloc] init];
    [self.topLab setFont:[UIFont fontWithName:FONT_NAME_LTH size:13]];
    [self.topLab setTextColor:[UIColor blackColor]];
    [self addSubview:self.topLab];
    
    self.contextLab = [[UILabel alloc] init];
    [self.contextLab setFont:[UIFont fontWithName:FONT_NAME_LTH size:13]];
    [self.contextLab setTextColor:[UIColor blackColor]];
    [self addSubview:self.contextLab];
    
    self.titleLab = [[UILabel alloc] init];
    self.titleLab.numberOfLines = 0;
    self.titleLab.userInteractionEnabled = YES;
    [self.titleLab setFont:[UIFont fontWithName:FONT_NAME_LTH size:15]];
    [self.titleLab setTextColor:[UIColor blueColor]];
    [self addSubview:self.titleLab];
    
    UITapGestureRecognizer *ges = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gesLabel:)];
    [self.titleLab addGestureRecognizer:ges];
    
}

#pragma mark - 重写set方法
-(void)setFrameModel:(XYJXCResultFrameModel *)frameModel{
    
    _frameModel = frameModel;
    
    XYJXCResultModel * model = frameModel.model;
    
    [self.cImageView sd_setImageWithURL:[NSURL URLWithString:model.xc_plan_days_image_url] placeholderImage:[UIImage imageNamed:@"zhanWeiTu"]];
    self.cImageView.frame = frameModel.imageViewFrame;
    
    self.topLab.text = model.xc_plan_days_memo;
    self.topLab.numberOfLines = 0;
    self.topLab.frame = frameModel.topLabFrame;
    
    if (![model.xc_plan_days_memo isEqualToString:@""]) {
        self.titleLab.text = @"今日备忘";
        self.titleLab.frame = frameModel.titleFrame;
    }else{
        
        self.titleLab.text = model.xc_plan_days_entry_name;
        self.titleLab.frame = frameModel.titleFrame;
    }
    NSString * string = model.xc_plan_days_tips;

    if ([string rangeOfString:@"#"].location !=NSNotFound) {
        NSRange startRange = [string rangeOfString:@"#"];
        NSRange endRange = [string rangeOfString:@" "];
        NSRange range = NSMakeRange(startRange.location + startRange.length, endRange.location - startRange.location - startRange.length);
        NSString *result = [string substringWithRange:range];

        NSMutableAttributedString * attributedString = [[NSMutableAttributedString alloc] initWithString:string];
        [attributedString addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Helvetica-Bold" size:14] range:NSMakeRange(0, result.length+2)];
        self.contextLab.attributedText = attributedString;
    }else{
        self.contextLab.text = model.xc_plan_days_tips;
    }
    self.contextLab.numberOfLines = 0;
    self.contextLab.frame = frameModel.contextFrame;
    
}

#pragma mark - 点击手势事件
-(void)gesLabel:(UITapGestureRecognizer *)tap{
    if (self.delegate && [self.delegate respondsToSelector:@selector(clickTitleLabel:)]) {
        [self.delegate clickTitleLabel:tap];
    }
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}


@end
